package com.mzy.linear_struct.hot100.动态规划;

/**
 * @Author: mizy3
 * @Date: 2024/09/07/下午9:21
 * @Description:
 */
public class Pro1035不相交的线 {
    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        // dp[i][j]表示当前位置有多少不相交的线
        int[][] dp = new int[nums1.length + 1][nums2.length + 1];

        for (int i = 1; i < dp.length; i++) {
            for (int j = 1; j < dp[i].length; j++) {
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < dp[i].length; j++) {
                System.out.print(dp[i][j] + " ");
            }
            System.out.println();
        }


        return dp[dp.length - 1][dp[0].length - 1];
    }

    public static void main(String[] args) {
        System.out.println(new Pro1035不相交的线().maxUncrossedLines(new int[]{1, 4, 2}, new int[]{1, 2, 4}));
    }
}























